1 % Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite
3 {\ttfamily \raggedright {
5 \mbox{}\textit{\textcolor{Brown
}{/*********
}} \\
6 \mbox{}\textit{\textcolor{Brown
}{\ *\ Point\ *
}} \\
7 \mbox{}\textit{\textcolor{Brown
}{\ *********
}} \\
8 \mbox{}\textit{\textcolor{Brown
}{\ *\ A\ simple\ point\ class\ used\ by\ some\ of\ the\ routines\ below.
}} \\
9 \mbox{}\textit{\textcolor{Brown
}{\ *\ Anything\ else\ that\ supports\ .x\ and\ .y\ will\ work\ just\ as
}} \\
10 \mbox{}\textit{\textcolor{Brown
}{\ *\ well.\ There\ are\
2\ variants\ -\ double\ and\ int.
}} \\
11 \mbox{}\textit{\textcolor{Brown
}{\ **/
}} \\
12 \mbox{}\textbf{\textcolor{Blue
}{struct
}}\ P\
\textcolor{Red
}{\
{}\
\textcolor{ForestGreen
}{double
}\ x
\textcolor{BrickRed
}{,
}\ y
\textcolor{BrickRed
}{;
}\
\textbf{\textcolor{Black
}{P
}}\textcolor{BrickRed
}{()
}\
\textcolor{Red
}{\
{\
}}\textcolor{BrickRed
}{;
}\
\textbf{\textcolor{Black
}{P
}}\textcolor{BrickRed
}{(
}\
\textcolor{ForestGreen
}{double
}\ q
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ w\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{:
}\
\textbf{\textcolor{Black
}{x
}}\textcolor{BrickRed
}{(
}\ q\
\textcolor{BrickRed
}{),
}\
\textbf{\textcolor{Black
}{y
}}\textcolor{BrickRed
}{(
}\ w\
\textcolor{BrickRed
}{)
}\
\textcolor{Red
}{\
{\
}}\
\textcolor{Red
}{\
}}\textcolor{BrickRed
}{;
} \\
13 \mbox{}\textbf{\textcolor{Blue
}{struct
}}\ P\
\textcolor{Red
}{\
{}\
\textcolor{ForestGreen
}{int
}\ x
\textcolor{BrickRed
}{,
}\ y
\textcolor{BrickRed
}{;
}\
\textbf{\textcolor{Black
}{P
}}\textcolor{BrickRed
}{()
}\
\textcolor{Red
}{\
{\
}}\textcolor{BrickRed
}{;
}\
\textbf{\textcolor{Black
}{P
}}\textcolor{BrickRed
}{(
}\
\textcolor{ForestGreen
}{int
}\ q
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{int
}\ w\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{:
}\
\textbf{\textcolor{Black
}{x
}}\textcolor{BrickRed
}{(
}\ q\
\textcolor{BrickRed
}{),
}\
\textbf{\textcolor{Black
}{y
}}\textcolor{BrickRed
}{(
}\ w\
\textcolor{BrickRed
}{)
}\
\textcolor{Red
}{\
{\
}}\
\textcolor{Red
}{\
}}\textcolor{BrickRed
}{;
} \\
15 \mbox{}\textit{\textcolor{Brown
}{/***************
}} \\
16 \mbox{}\textit{\textcolor{Brown
}{\ *\ Polar\ angle\ *
}} \\
17 \mbox{}\textit{\textcolor{Brown
}{\ ***************
}} \\
18 \mbox{}\textit{\textcolor{Brown
}{\ *\ Returns\ an\ angle\ in\ the\ range\
[0,\
2*Pi)\ of\ a\ given\ Cartesian\ point.
}} \\
19 \mbox{}\textit{\textcolor{Brown
}{\ *\ If\ the\ point\ is\ (
0,
0),\ -
1.0\ is\ returned.
}} \\
20 \mbox{}\textit{\textcolor{Brown
}{\ *\ REQUIRES:
}} \\
21 \mbox{}\textit{\textcolor{Brown
}{\ *\ include\ math.h
}} \\
22 \mbox{}\textit{\textcolor{Brown
}{\ *\ define\ EPS\
0.000000001,\ or\ your\ choice
}} \\
23 \mbox{}\textit{\textcolor{Brown
}{\ *\ P\ has\ members\ x\ and\ y.
}} \\
24 \mbox{}\textit{\textcolor{Brown
}{\ **/
}} \\
25 \mbox{}\textcolor{ForestGreen
}{double
}\
\textbf{\textcolor{Black
}{polarAngle
}}\textcolor{BrickRed
}{(
}\ P\ p\
\textcolor{BrickRed
}{)
} \\
26 \mbox{}\textcolor{Red
}{\
{} \\
27 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{if
}}\textcolor{BrickRed
}{(
}\
\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}\ p
\textcolor{BrickRed
}{.
}x\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$=
}\ EPS\
\textcolor{BrickRed
}{\&\&
}\
\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}\ p
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$=
}\ EPS\
\textcolor{BrickRed
}{)
}\
\textbf{\textcolor{Blue
}{return
}}\
\textcolor{BrickRed
}{-
}\textcolor{Purple
}{1.0}\textcolor{BrickRed
}{;
} \\
28 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{if
}}\textcolor{BrickRed
}{(
}\
\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}\ p
\textcolor{BrickRed
}{.
}x\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$=
}\ EPS\
\textcolor{BrickRed
}{)
}\
\textbf{\textcolor{Blue
}{return
}}\
\textcolor{BrickRed
}{(
}\ p
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{$>$
}\ EPS\
\textcolor{BrickRed
}{?
}\
\textcolor{Purple
}{1.0}\
\textcolor{BrickRed
}{:
}\
\textcolor{Purple
}{3.0}\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{);
} \\
29 \mbox{}\ \ \ \
\textcolor{ForestGreen
}{double
}\ theta\
\textcolor{BrickRed
}{=
}\
\textbf{\textcolor{Black
}{atan
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{1.0}\
\textcolor{BrickRed
}{*
}\ p
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{/
}\ p
\textcolor{BrickRed
}{.
}x\
\textcolor{BrickRed
}{);
} \\
30 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{if
}}\textcolor{BrickRed
}{(
}\ p
\textcolor{BrickRed
}{.
}x\
\textcolor{BrickRed
}{$>$
}\ EPS\
\textcolor{BrickRed
}{)
}\
\textbf{\textcolor{Blue
}{return
}}\textcolor{BrickRed
}{(
}\ p
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{$>$=
}\
\textcolor{BrickRed
}{-
}EPS\
\textcolor{BrickRed
}{?
}\ theta\
\textcolor{BrickRed
}{:
}\
\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{4}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{+
}\ theta\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{);
} \\
31 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{return
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{2}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{+
}\ theta\
\textcolor{BrickRed
}{);
} \\
32 \mbox{}\textcolor{Red
}{\
}} \\
34 \mbox{}\textit{\textcolor{Brown
}{/************************
}} \\
35 \mbox{}\textit{\textcolor{Brown
}{\ *\ Point\ inside\ polygon\ *
}} \\
36 \mbox{}\textit{\textcolor{Brown
}{\ ************************
}} \\
37 \mbox{}\textit{\textcolor{Brown
}{\ *\ Returns\ true\ iff\ p\ is\ inside\ poly.
}} \\
38 \mbox{}\textit{\textcolor{Brown
}{\ *\ PRE:\ The\ vertices\ of\ poly\ are\ ordered\ (either\ clockwise\ or
}} \\
39 \mbox{}\textit{\textcolor{Brown
}{\ *\ \ \ \ \ \ counter-clockwise.
}} \\
40 \mbox{}\textit{\textcolor{Brown
}{\ *\ POST:\ Modify\ code\ inside\ to\ handle\ the\ special\ case\ of\ "
{}on\ an\ edge"
{}.
}} \\
41 \mbox{}\textit{\textcolor{Brown
}{\ *\ REQUIRES:
}} \\
42 \mbox{}\textit{\textcolor{Brown
}{\ *\ polarAngle()
}} \\
43 \mbox{}\textit{\textcolor{Brown
}{\ *\ include\ math.h
}} \\
44 \mbox{}\textit{\textcolor{Brown
}{\ *\ include\ vector
}} \\
45 \mbox{}\textit{\textcolor{Brown
}{\ *\ define\ EPS\
0.000000001,\ or\ your\ choice
}} \\
46 \mbox{}\textit{\textcolor{Brown
}{\ **/
}} \\
47 \mbox{}\textcolor{ForestGreen
}{bool
}\
\textbf{\textcolor{Black
}{pointInPoly
}}\textcolor{BrickRed
}{(
}\ P\ p
\textcolor{BrickRed
}{,
}\ vector
\textcolor{BrickRed
}{$<$
}\ P\
\textcolor{BrickRed
}{$>$
}\
\textcolor{BrickRed
}{\&
}poly\
\textcolor{BrickRed
}{)
} \\
48 \mbox{}\textcolor{Red
}{\
{} \\
49 \mbox{}\ \ \ \
\textcolor{ForestGreen
}{int
}\ n\
\textcolor{BrickRed
}{=
}\ poly
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{size
}}\textcolor{BrickRed
}{();
} \\
50 \mbox{}\ \ \ \
\textcolor{ForestGreen
}{double
}\ ang\
\textcolor{BrickRed
}{=
}\
\textcolor{Purple
}{0.0}\textcolor{BrickRed
}{;
} \\
51 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{for
}}\textcolor{BrickRed
}{(
}\
\textcolor{ForestGreen
}{int
}\ i\
\textcolor{BrickRed
}{=
}\ n\
\textcolor{BrickRed
}{-
}\
\textcolor{Purple
}{1}\textcolor{BrickRed
}{,
}\ j\
\textcolor{BrickRed
}{=
}\
\textcolor{Purple
}{0}\textcolor{BrickRed
}{;
}\ j\
\textcolor{BrickRed
}{$<$
}\ n
\textcolor{BrickRed
}{;
}\ i\
\textcolor{BrickRed
}{=
}\ j
\textcolor{BrickRed
}{++
}\
\textcolor{BrickRed
}{)
} \\
52 \mbox{}\ \ \ \
\textcolor{Red
}{\
{} \\
53 \mbox{}\ \ \ \ \ \ \ \ P\
\textbf{\textcolor{Black
}{v
}}\textcolor{BrickRed
}{(
}\ poly
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{].
}x\
\textcolor{BrickRed
}{-
}\ p
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{,
}\ poly
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{].
}y\
\textcolor{BrickRed
}{-
}\ p
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{);
} \\
54 \mbox{}\ \ \ \ \ \ \ \ P\
\textbf{\textcolor{Black
}{w
}}\textcolor{BrickRed
}{(
}\ poly
\textcolor{BrickRed
}{[}j
\textcolor{BrickRed
}{].
}x\
\textcolor{BrickRed
}{-
}\ p
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{,
}\ poly
\textcolor{BrickRed
}{[}j
\textcolor{BrickRed
}{].
}y\
\textcolor{BrickRed
}{-
}\ p
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{);
} \\
55 \mbox{}\ \ \ \ \ \ \ \
\textcolor{ForestGreen
}{double
}\ va\
\textcolor{BrickRed
}{=
}\
\textbf{\textcolor{Black
}{polarAngle
}}\textcolor{BrickRed
}{(
}\ v\
\textcolor{BrickRed
}{);
} \\
56 \mbox{}\ \ \ \ \ \ \ \
\textcolor{ForestGreen
}{double
}\ wa\
\textcolor{BrickRed
}{=
}\
\textbf{\textcolor{Black
}{polarAngle
}}\textcolor{BrickRed
}{(
}\ w\
\textcolor{BrickRed
}{);
} \\
57 \mbox{}\ \ \ \ \ \ \ \
\textcolor{ForestGreen
}{double
}\ xx\
\textcolor{BrickRed
}{=
}\ wa\
\textcolor{BrickRed
}{-
}\ va
\textcolor{BrickRed
}{;
} \\
58 \mbox{}\ \ \ \ \ \ \ \
\textbf{\textcolor{Blue
}{if
}}\textcolor{BrickRed
}{(
}\ va\
\textcolor{BrickRed
}{$<$
}\
\textcolor{BrickRed
}{-
}\textcolor{Purple
}{0.5}\
\textcolor{BrickRed
}{$|$$|$
}\ wa\
\textcolor{BrickRed
}{$<$
}\
\textcolor{BrickRed
}{-
}\textcolor{Purple
}{0.5}\
\textcolor{BrickRed
}{$|$$|$
}\
\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}\
\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}\ xx\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{-
}\
\textcolor{Purple
}{2}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$
}\ EPS\
\textcolor{BrickRed
}{)
} \\
59 \mbox{}\ \ \ \ \ \ \ \
\textcolor{Red
}{\
{} \\
60 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \
\textit{\textcolor{Brown
}{//\ POINT\ IS\ ON\ THE\ EDGE
}} \\
61 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \
\textbf{\textcolor{Black
}{assert
}}\textcolor{BrickRed
}{(
}\
\textbf{\textcolor{Blue
}{false
}}\
\textcolor{BrickRed
}{);
} \\
62 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ ang\
\textcolor{BrickRed
}{+=
}\
\textcolor{Purple
}{2}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{);
} \\
63 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \
\textbf{\textcolor{Blue
}{continue
}}\textcolor{BrickRed
}{;
} \\
64 \mbox{}\ \ \ \ \ \ \ \
\textcolor{Red
}{\
}} \\
65 \mbox{}\ \ \ \ \ \ \ \
\textbf{\textcolor{Blue
}{if
}}\textcolor{BrickRed
}{(
}\ xx\
\textcolor{BrickRed
}{$<$
}\
\textcolor{BrickRed
}{-
}\textcolor{Purple
}{2}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{)
}\ ang\
\textcolor{BrickRed
}{+=
}\ xx\
\textcolor{BrickRed
}{+
}\
\textcolor{Purple
}{4}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{);
} \\
66 \mbox{}\ \ \ \ \ \ \ \
\textbf{\textcolor{Blue
}{else
}}\
\textbf{\textcolor{Blue
}{if
}}\textcolor{BrickRed
}{(
}\ xx\
\textcolor{BrickRed
}{$>$
}\
\textcolor{Purple
}{2}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{)
}\ ang\
\textcolor{BrickRed
}{+=
}\ xx\
\textcolor{BrickRed
}{-
}\
\textcolor{Purple
}{4}\
\textcolor{BrickRed
}{*
}\
\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\
\textcolor{Purple
}{0}\
\textcolor{BrickRed
}{);
} \\
67 \mbox{}\ \ \ \ \ \ \ \
\textbf{\textcolor{Blue
}{else
}}\ ang\
\textcolor{BrickRed
}{+=
}\ xx
\textcolor{BrickRed
}{;
} \\
68 \mbox{}\ \ \ \
\textcolor{Red
}{\
}} \\
69 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{return
}}\textcolor{BrickRed
}{(
}\ ang\
\textcolor{BrickRed
}{*
}\ ang\
\textcolor{BrickRed
}{$>$
}\
\textcolor{Purple
}{1.0}\
\textcolor{BrickRed
}{);
} \\
70 \mbox{}\textcolor{Red
}{\
}} \\
72 } \normalfont\normalsize